package com.zang.blogz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zang.blogz.entity.RoleMenu;

import com.zang.blogz.mapper.RoleMenuMapper;
import com.zang.blogz.service.RoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.github.vampireachao.stream.core.stream.Steam;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 朵橙i
 * @since 2022-06-17
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {

    @Override
    public List<Integer> getMenuIds(Collection<Integer> collection) {

        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.in(RoleMenu::getRoleId,collection);

        return Steam.of(list(queryWrapper)).map(RoleMenu::getMenuId).toList() ;
    }

    @Override
    public void deleteMenu(Integer roleId) {

        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(RoleMenu::getRoleId, roleId);

        remove(queryWrapper);
    }

    @Override
    public List<RoleMenu> addRoleMenu(Integer roleId, List<Integer> menuIds) {

        List<RoleMenu> roleMenus = new ArrayList<>(menuIds.size());

        menuIds.forEach(menuId -> {

            RoleMenu roleMenu = new RoleMenu();

            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);

            roleMenus.add(roleMenu);
        });

        saveBatch(roleMenus);

        return roleMenus;
    }
}
